﻿#include "FrozenAxeman.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FrozenAxeman::FrozenAxeman(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool FrozenAxeman::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void FrozenAxeman::Attack()
    {

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        bool range = !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!range && getEnvir()->Random->Next(3) > 0)
        {
            if (getEnvir()->getTime() >= PullTime)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 2;
                Broadcast(tempVar);
                PullTime = getEnvir()->getTime() + 10000;
                AttackTime = getEnvir()->getTime() + AttackSpeed;
                ActionTime = getEnvir()->getTime() + 300;

                getTarget()->Pushed(this, Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()), 2 + getEnvir()->Random->Next(3));
                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::AC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                MonsterObject::Attack();
            }
        }
        else
        {

            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);
            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage * 2, DefenceType::AC});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }


        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;


    }
}
